<template>
  <div class="goods-image">
    <!-- 大图 -->
    <div
      v-show="isShow"
      class="large"
      :style="{
        backgroundImage: `url(${goodsImage[currentIndex]})`,
        backgroundPositionX: -2 * x + 'px',
        backgroundPositionY: -2 * y + 'px'
      }"
    ></div>
    <!-- 小图 -->
    <div class="middle" ref="target">
      <img :src="goodsImage[currentIndex]" alt="" />
      <div
        class="layer"
        :style="{ left: x + 'px', top: y + 'px' }"
        v-show="isShow"
      ></div>
    </div>
    <!-- 侧边的小图 -->
    <ul class="small">
      <li
        v-for="(item, index) in goodsImage"
        :key="index"
        @mouseenter="currentIndex = index"
        :class="{ active: currentIndex === index }"
      >
        <img :src="item" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
import { reactive, ref, watch } from 'vue'
import { toRefs, useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    goodsImage: {
      type: Array,
      default: () => []
    }
  },
  setup (props) {
    const currentIndex = ref(0)
    const target = ref(null)
    const layerPosition = reactive({
      x: 200,
      y: 0,
      isShow: false
    })
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    watch(
      [elementX, elementY, isOutside],
      () => {
        // console.log(isOutside.value)
        // console.log(elementX, elementY, isOutside)
        layerPosition.isShow = !isOutside.value
        if (!isOutside.value) {
          layerPosition.x = elementX.value - 100
          layerPosition.y = elementY.value - 100
          //   console.log(2)
          //   console.log(layerPosition.x)
          if (elementX.value < 100) {
            layerPosition.x = 0
          }
          if (elementX.value > 300) {
            layerPosition.x = 200
          }
          if (elementY.value < 100) {
            layerPosition.y = 0
          }
          if (elementY.value > 300) {
            layerPosition.y = 200
          }
        }
      }
      //   { immediate: true }
    )

    return {
      currentIndex,
      elementX,
      elementY,
      isOutside,
      target,
      ...toRefs(layerPosition)
    }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 500;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.4);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
